﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class MapSelfAdaptaionTool : MonoBehaviour
{
    private readonly float m_GAME_RECT_RATIO = 0.83f;           //  food range in map.

    [SerializeField]
    private Camera m_3dMapCamera;
    [SerializeField]
    private Transform m_3dBgObj;
    [SerializeField]
    private Texture m_bgTexture;

    [SerializeField]
    private BoxCollider m_upCollider;
    [SerializeField]
    private BoxCollider m_downCollider;
    [SerializeField]
    private BoxCollider m_leftCollider;
    [SerializeField]
    private BoxCollider m_rightCollider;


    public static FPRIWS_V2 viewRect { get; private set; }           //摄像机范围
    public static FPRIWS_V2 gameRect { get; private set; }           //有效游戏空间范围
    public static Vector2 gameCenter { get; private set; }

    private void Awake()
    {
        Debug.DrawLine(Vector3.zero, Vector3.one);
        m_MapAdaptation();
    }



    /// <summary>
    /// 相机 与 地图的自适应的方法
    /// </summary>
    /// 
    [ContextMenu("Editor Adaptation")]
    private void m_MapAdaptation()
    {
        viewRect = UtilityFunctions.GetCameraRectPoint_X_Y_Dimensional(m_3dMapCamera);
        gameRect = m_GetGameRect(viewRect, m_GAME_RECT_RATIO);
        m_SetBgMap(viewRect);
        m_SetCollider(gameRect);
        Debug.Log(viewRect);
        Debug.Log(gameRect);
    }
    private static FPRIWS_V2 m_GetGameRect(FPRIWS_V2 pViewRect, float pGameRectRatio)
    {
        var cameraRealRect = pViewRect.U_R - pViewRect.D_L;
        var gameRect = new FPRIWS_V2()
        {
            U_L = pViewRect.U_L,
            D_L = pViewRect.D_L,
            U_R = new Vector2()
            {
                x = pViewRect.D_L.x + cameraRealRect.x * pGameRectRatio,
                y = pViewRect.U_R.y
            },
            D_R = new Vector2()
            {
                x = pViewRect.D_L.x + cameraRealRect.x * pGameRectRatio,
                y = pViewRect.D_R.y
            },
        };
        gameCenter = (gameRect.D_L + gameRect.U_L) / 2;
        return gameRect;
    }

    private void m_SetBgMap(FPRIWS_V2 pViewRect)
    {
        var mapSize = pViewRect.U_R - pViewRect.D_L;
        var screenW = m_3dMapCamera.pixelWidth;
        var screenH = m_3dMapCamera.pixelHeight;
        var screenRatio = (float)screenW / screenH;
        var textureW = m_bgTexture.width;
        var textureH = m_bgTexture.height;
        var texureRatio = (float)textureW / textureH;
        if (screenRatio > texureRatio)
            mapSize.y = mapSize.x / texureRatio;
        else if (screenRatio < texureRatio)
            mapSize.x = mapSize.y * texureRatio;
        m_3dBgObj.localScale = mapSize;
    }

    private void m_SetCollider(FPRIWS_V2 pGameRect)
    {
        var mapSize = pGameRect.U_R - pGameRect.D_L;
        m_upCollider.transform.position = (pGameRect.U_R + pGameRect.U_L) / 2;
        m_upCollider.size = new Vector3(mapSize.x, 0.1f, 1f);
        m_upCollider.center = new Vector3();
        m_downCollider.transform.position = (pGameRect.D_R + pGameRect.D_L) / 2;
        m_downCollider.size = new Vector3(mapSize.x, 0.1f, 1f);
        m_leftCollider.transform.position = (pGameRect.U_L + pGameRect.D_L) / 2;
        m_leftCollider.size = new Vector3(0.1f, mapSize.y, 1f);
        m_rightCollider.transform.position = (pGameRect.U_R + pGameRect.D_R) / 2;
        m_rightCollider.size = new Vector3(0.1f, mapSize.y, 1f);
    }

    private void OnDrawGizmos()
    {
        if (viewRect != null)
        {
            Gizmos.color = Color.red;
            Gizmos.DrawLine(viewRect.D_L, viewRect.D_R);
            Gizmos.DrawLine(viewRect.D_L, viewRect.U_L);
            Gizmos.DrawLine(viewRect.U_L, viewRect.U_R);
            Gizmos.DrawLine(viewRect.U_R, viewRect.D_R);
        }
        if (gameRect != null)
        {
            Gizmos.color = Color.blue;
            Gizmos.DrawLine(gameRect.D_L, gameRect.D_R);
            Gizmos.DrawLine(gameRect.D_L, gameRect.U_L);
            Gizmos.DrawLine(gameRect.U_L, gameRect.U_R);
            Gizmos.DrawLine(gameRect.U_R, gameRect.D_R);
        }
    }
}
